package com.fc.test.service;

import java.util.List;
import java.util.Arrays;

import cn.hutool.db.sql.Condition;
import cn.hutool.db.sql.SqlUtil;
import com.fc.test.model.auto.HandleCase;
import com.fc.test.model.auto.HandleCaseExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.PolicyMapper;
import com.fc.test.model.auto.Policy;
import com.fc.test.model.auto.PolicyExample;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 * 惠民政策 PolicyService
 * @Title: PolicyService.java 
 * @Package com.fc.test.service 
 * @author fuce_自动生成
 * @email 1345625601@qq.com
 * @date 2020-11-06 20:15:50  
 **/
@Service
public class PolicyService implements BaseService<Policy, PolicyExample>{
	@Autowired
	private PolicyMapper policyMapper;
	
      	   	      	      	      	      	
	/**
	 * 分页查询
	 * @param tablepar
	 * @param searchText
	 * @return
	 */
	 public PageInfo<Policy> list(Tablepar tablepar,String searchText){
	        PolicyExample testExample=new PolicyExample();
	        testExample.setOrderByClause("id ASC");
	        if(StrUtil.isNotEmpty(searchText)){
	        	testExample.createCriteria().andTitleLike("%"+searchText+"%");
	        }
			if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<Policy> list= policyMapper.selectByExample(testExample);
	        PageInfo<Policy> pageInfo = new PageInfo<Policy>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			PolicyExample example=new PolicyExample();
			example.createCriteria().andIdIn(stringB);
			return policyMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Policy selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return policyMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Policy record) {
		return policyMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Policy record) {
				
		record.setId(null);
		
				
		return policyMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Policy record, PolicyExample example) {
		
		return policyMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Policy record, PolicyExample example) {
		
		return policyMapper.updateByExample(record, example);
	}

	@Override
	public List<Policy> selectByExample(PolicyExample example) {
		
		return policyMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(PolicyExample example) {
		
		return policyMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(PolicyExample example) {
		
		return policyMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param policy
	 * @return
	 */
	public int checkNameUnique(Policy policy){
		PolicyExample example=new PolicyExample();
		example.createCriteria().andTitleEqualTo(policy.getTitle());
		List<Policy> list=policyMapper.selectByExample(example);
		return list.size();
	}


	public PageInfo<Policy> list2(int pageNum, int pageSize, String searchText) {
		PageHelper.startPage(pageNum, pageSize);

		PolicyExample testExample=new PolicyExample();
		testExample.setOrderByClause("id ASC");
		 PolicyExample.Criteria criteria = testExample.createCriteria();
		criteria.andStatusEqualTo(1);
		if(StrUtil.isNotBlank(searchText)){
			criteria.andTitleLike("%"+searchText.trim()+"%");
			PolicyExample.Criteria criteria1 = testExample.createCriteria();
			criteria1.andStatusEqualTo(1);
			criteria1.andContentLike("%"+searchText.trim()+"%");

			testExample.or(criteria1);
		}
		
		PageHelper.startPage(pageNum, pageSize);
		List<Policy> list= policyMapper.selectByExample(testExample);
		PageInfo<Policy> pageInfo = new PageInfo<>(list);
		return  pageInfo;
	}
}
